package com.example.calendar.ui.screens.onboarding

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CalendarMonth
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.Sync
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * Onboarding screen shown to users on first launch
 */
@Composable
fun OnboardingScreen(
    navController: NavController,
    viewModel: OnboardingViewModel = hiltViewModel()
) {
    var currentPage by remember { mutableStateOf(0) }
    val scope = rememberCoroutineScope()
    val pageCount = 4

    // Define onboarding pages
    val pages = listOf(
        OnboardingPage(
            title = "Welcome to Calendar",
            description = "Your all-in-one solution for managing events, tasks, and staying organized.",
            icon = Icons.Default.CalendarMonth,
            iconTint = MaterialTheme.colorScheme.primary
        ),
        OnboardingPage(
            title = "Event Management",
            description = "Create, edit, and manage events. View your schedule in month, week, or day views.",
            icon = Icons.Default.CalendarMonth,
            iconTint = MaterialTheme.colorScheme.secondary
        ),
        OnboardingPage(
            title = "Task Management",
            description = "Track your to-dos, set due dates, and never miss important tasks.",
            icon = Icons.Default.CheckCircle,
            iconTint = MaterialTheme.colorScheme.tertiary
        ),
        OnboardingPage(
            title = "Get Started",
            description = "You're all set! Start using the calendar to organize your life.",
            icon = Icons.Default.Sync,
            iconTint = MaterialTheme.colorScheme.primary
        )
    )

    Box(
        modifier = Modifier.fillMaxSize()
    ) {
        // Current page content with animation
        pages.forEachIndexed { index, page ->
            AnimatedVisibility(
                visible = currentPage == index,
                enter = fadeIn(animationSpec = tween(300)) +
                        slideInHorizontally(
                            initialOffsetX = { it },
                            animationSpec = tween(300)
                        ),
                exit = fadeOut(animationSpec = tween(300)) +
                        slideOutHorizontally(
                            targetOffsetX = { -it },
                            animationSpec = tween(300)
                        )
            ) {
                OnboardingPageContent(
                    page = page,
                    modifier = Modifier.fillMaxSize()
                )
            }
        }

        // Bottom navigation and indicators
        Column(
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(16.dp)
        ) {
            // Page indicators
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.Center
            ) {
                repeat(pageCount) { index ->
                    PageIndicator(
                        isSelected = index == currentPage,
                        modifier = Modifier.padding(horizontal = 4.dp)
                    )
                }
            }

            Spacer(modifier = Modifier.height(24.dp))

            // Navigation buttons
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                // Skip button (not shown on last page)
                if (currentPage < pageCount - 1) {
                    TextButton(
                        onClick = {
                            scope.launch {
                                viewModel.completeOnboarding()
                                navController.navigate("month") {
                                    popUpTo("onboarding") { inclusive = true }
                                }
                            }
                        }
                    ) {
                        Text("Skip")
                    }
                } else {
                    // Empty spacer for alignment
                    Spacer(modifier = Modifier.width(64.dp))
                }

                // Next/Get Started button
                Button(
                    onClick = {
                        if (currentPage < pageCount - 1) {
                            currentPage++
                        } else {
                            scope.launch {
                                viewModel.completeOnboarding()
                                navController.navigate("month") {
                                    popUpTo("onboarding") { inclusive = true }
                                }
                            }
                        }
                    }
                ) {
                    Text(if (currentPage < pageCount - 1) "Next" else "Get Started")
                }
            }

            Spacer(modifier = Modifier.height(32.dp))
        }
    }
}

/**
 * Content for a single onboarding page
 */
@Composable
fun OnboardingPageContent(
    page: OnboardingPage,
    modifier: Modifier = Modifier
) {
    Column(
        modifier = modifier.padding(24.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        // Icon
        Icon(
            imageVector = page.icon,
            contentDescription = null,
            tint = page.iconTint,
            modifier = Modifier.size(120.dp)
        )

        Spacer(modifier = Modifier.height(48.dp))

        // Title
        Text(
            text = page.title,
            style = MaterialTheme.typography.headlineMedium,
            fontWeight = FontWeight.Bold,
            textAlign = TextAlign.Center
        )

        Spacer(modifier = Modifier.height(16.dp))

        // Description
        Text(
            text = page.description,
            style = MaterialTheme.typography.bodyLarge,
            textAlign = TextAlign.Center,
            color = MaterialTheme.colorScheme.onSurfaceVariant
        )
    }
}

/**
 * Page indicator dot
 */
@Composable
fun PageIndicator(
    isSelected: Boolean,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier
            .size(if (isSelected) 12.dp else 8.dp)
            .clip(CircleShape)
            .background(
                if (isSelected) MaterialTheme.colorScheme.primary
                else MaterialTheme.colorScheme.onSurface.copy(alpha = 0.2f)
            )
    )
}

/**
 * Data class for onboarding page content
 */
data class OnboardingPage(
    val title: String,
    val description: String,
    val icon: ImageVector,
    val iconTint: Color
) 